package com.flashflexpro.flexdirector.model{
    import flash.display.BitmapData;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;

    import mx.utils.ObjectUtil;


    /**
     * abstract
     *
     */
    [Bindable]
    [RemoteClass]
    public class DtoAction extends NameAndComment{

        public function DtoAction(){
            name = "Unnamed action";
        }

        public var waitMilsec:int = 0;
        /**
         * not null
         */
        public var appInst:DtoAppInst;


        /**
         * if it's a check or input
         */
        public var comInst:DtoFlexComInst;


        public var check:DtoActCheck;
        public var input:DtoActInput;

        public function getProperty( propertyName:String ):CodeStringValue{
            if( check != null ){
                for each ( var csv:CodeStringValue in check.allProperties ){
                    if( csv.desc.name == propertyName ){
                        return csv;
                    }
                }
            }
            else{
                if( input != null ){
                    for each ( var argCsv:CodeStringValue in input.args ){
                        if( argCsv.desc.name == propertyName ){
                            return argCsv;
                        }
                    }
                }
            }
            throw new Error( this + ".getProperty found none!" );
        }


        /**
         * >0 ----- it's starting new appInst
         * <0 ----- it's ending existing appInst;
         */
        public var edge:int = 0;


        override public function copyInto( parent:NameAndComment, newType:Class ):NameAndComment{
            CONFIG::debugging{
                if( !( parent is DtoTestProcedure ) ){
                    throw new Error( this + ".copyInto " );
                }
                if( newType != DtoActionReplay ){
                    throw new Error( this + ".copyInto " );
                }
            }
            var rt:DtoAction = super.copyInto( parent, newType ) as DtoAction;

            rt.waitMilsec = waitMilsec;

            rt.comInst = comInst;
            if( check != null ){
                rt.check = check.copyInto( rt, DtoActCheck ) as DtoActCheck;
            }
            if( input != null ){
                rt.input = input.copyInto( rt, DtoActInput ) as DtoActInput;
            }
            rt.edge = edge;
            rt.recordingWidth = recordingWidth;
            rt.recordingHeight = recordingHeight;
            rt.recordingBitMap = ObjectUtil.copy( recordingBitMap ) as ByteArray;
            return rt;
        }


        public var recordingBitMap:ByteArray;
        public var recordingWidth:int;
        public var recordingHeight:int;

        public function getBitMap():BitmapData{
            if( recordingBitMap == null ){
                return null;
            }
            var rt:BitmapData = new BitmapData( recordingWidth, recordingHeight );
            var ba:ByteArray = ObjectUtil.copy( recordingBitMap ) as ByteArray;
            ba.position = 0;
            ba.uncompress();
            ba.position = 0;
            rt.setPixels( new Rectangle( 0, 0, recordingWidth, recordingHeight ), ba );
            return rt;
        }

        override public function destroy():void{
            comInst = null;
            appInst = null;
            check = null;
            input = null;
        }

    }
}